package com.example.tbplus14.utils.html;


import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;

public class HtmlToImgJava {

    public static void main(String[] args) throws Exception{
        // 创建JFrame并设置其大小为HTML内容的大小
        JFrame frame = new JFrame();
        frame.setSize(800, 600); // 根据需要调整大小
        frame.setLocationRelativeTo(null); // 居中显示
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true); // 无边框窗口

        // 创建JEditorPane并加载HTML内容
        JEditorPane editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        String html = html();
        editorPane.setText(html);
        editorPane.setSize(800, 600); // 设置与JFrame相同的大小

        // 将JEditorPane添加到JFrame中，但不显示它（因为我们只想要截图）
        frame.getContentPane().add(editorPane);
        frame.setVisible(true);
        frame.pack(); // 根据组件调整大小

        // 使用Robot类捕获屏幕截图
        try {
            Robot robot = new Robot();
            Rectangle captureSize = new Rectangle(frame.getSize());
            BufferedImage capture = robot.createScreenCapture(captureSize);
            ImageIO.write(capture, "PNG", new File("C:\\Users\\tb14plus\\Desktop\\log\\aacc.png")); // 保存为PNG文件
        } catch (AWTException | IOException e) {
            e.printStackTrace();
        } finally {
            frame.dispose(); // 清理资源
        }
    }




    private static String html()throws Exception{
        ClassPathResource classPathResourceBg = new ClassPathResource("bodybg.png");
        String imageBgPath = classPathResourceBg.getURL().getPath();
        String HtmlTemplateStr = "<div class=\"all\" id=\"all\" style=\"display: flex;justify-content: center;\">\n" +
                "  <div style=\"width: 375px; background: url("+imageBgPath+") no-repeat; font-family: PingFangSC-Regular, PingFang SC; font-size: 12px;\" id=\"card\">\n" +
                "    <div class=\"anda\" style=\"margin-top: 14px; text-align: center;height: 20px;font-size: 14px;font-family: PingFangSC-Medium, PingFang SC;font-weight: 500;color: #FFFFFF;line-height: 20px;\">\n" +
                "      <span>Your Virtual Collection Account</span>\n" +
                "    </div>\n" +
                "    <div style=\"background-color: #ffffff;width: 93%;margin: 14px auto 0;border-radius:8px;padding-top: 14px;padding-bottom: 12px;\">\n" +
                "      <div class=\"card\" style=\"width: 91%;height: 182px;margin: 0 auto;background-size:100%;border-radius:4px\">\n" +
                "        <img id=\"pic\" src=\"\" alt=\"\" style=\"height: 182px\" />\n" +
                "      </div>\n" +
                "      <div class=\"tip\" style=\"width: 310px;height: 34px;font-size: 12px;font-family: PingFangSC-Regular, PingFang SC;font-weight: 400;color: #9b9898;line-height: 17px;margin: 14px auto 0;\">\n" +
                "        Your Virtual Collection Account has been opened and you can receive Instapay and Pesonet remittances through your Virtual Collection Account.\n" +
                "      </div>\n" +
                "      <div class=\"divisi\" style=\"width: 303px;height: 1px;background: #D8D8D8;margin: 21px auto 0;\"></div>\n" +
                "      <div class=\"Nomor\" style=\"display: flex;margin-left: 23px;margin-top: 8px;\">\n" +
                "        <span style=\"width: 144px;display: flex;justify-content: space-between;\">Account number<span>:</span></span>\n" +
                "        <span id=\"NomorNum\">&nbsp;&nbsp;</span>\n" +
                "      </div>\n" +
                "      <div class=\"Name\" style=\"display:flex;height:auto;flex-direction:row;margin-left: 23px;margin-top: 3px;padding-right: 1px;align-items: flex-start;\">\n" +
                "        <span style=\"width: 144px;display: flex;justify-content: space-between;\">Name<span>:</span></span>\n" +
                "        <span id=\"NameDetail\">&nbsp;&nbsp;</span>\n" +
                "      </div>\n" +
                "      <div class=\"Name\" style=\"display:flex;height:auto;flex-direction:row;margin-left: 23px;margin-top: 3px;padding-right: 1px;align-items: flex-start;\">\n" +
                "        <span style=\"width: 144px;display: flex;justify-content: space-between;\">Identification Number <span>:</span></span>\n" +
                "        <span id=\"KartNum\">&nbsp;&nbsp;</span>\n" +
                "      </div>\n" +
                "      <div class=\"Name\" style=\"display:flex;height:auto;flex-direction:row;margin-left: 23px;margin-top: 3px;padding-right: 1px;align-items: flex-start;\">\n" +
                "        <span style=\"width: 144px;display: flex;justify-content: space-between;\">Registered bank <span>:</span></span>\n" +
                "        <span class=\"NameDetail\" style=\"width: 166px;word-break: break-all;float: right;margin-top: 4px;font-size: 12px;\" id=\"CabangNum\">&nbsp;&nbsp;</span>\n" +
                "      </div>\n" +
                "      <div class=\"Notice\" style=\"margin-top: 12px;font-size: 12px;color: #9b9898;  width: 97%;padding-left: 2%;\">\n" +
                "        Please check your Virtual Collection Account information. If you have any questions, please seek our help in time\n" +
                "      </div>\n" +
                "    </div>\n" +
                "    <span class=\"power\" style=\"width:100%;text-align: center;display:block;font-size: 12px;font-family: PingFangSC-Regular, PingFang SC;font-weight: 300;color: #e6c6c6;line-height: 20px;margin-top: 10px;margin-bottom: 10px;\">Power by Netbank</span>\n" +
                "  </div>\n" +
                "</div>";
        return HtmlTemplateStr;
    }


}
